<?php
/** Zend_Controller_Action */ 
require_once ROOT_PATH . '/library/Soooner/Controller/Action.php';
class EpgController extends Soooner_Controller_Action
{
	private $_epgid;
	private $_modelEpg;
	private $_modelContent;
    public function init()
    {
        /* Initialize action controller here */
    	$this->initDzTemplate();
    	$this->_modelEpg = new Model_Tables_Epg();
    	//$this->_modelContent = new Model_Tables_Content();
    }
    /**
    * 获取节目单列表
    * @return unknown_type
    */
    public function indexAction()
    {
//    	$options = $GLOBALS['application']->getOptions();
//    	$numPerPage = $options["pagecount"];//每页显示的条数
//        $ItemCount =  $this->_modelEpg->getCount();
//        $PageCount = ceil($ItemCount/$numPerPage);
//        $PageCount = $PageCount ? $PageCount : 1;
		$contentid = $this->_getParam('contentid'); 
		$t = $this->_getParam('t'); 
		$date = date('Y-m-d');
		include $this->_dzTemplate->getFile( 'epg.manage.html' );
    }
	public function testAction()
    {
		$this->indexAction();
    }
    /**
    * 获取节目单列表
    * @return unknown_type
    */
    public function indexajaxAction()
    {
    	$contentid = $this->_getParam('contentid'); 
		$t = $this->_getParam('t') ; 
        $options = $GLOBALS['application']->getOptions();
    	$numPerPage = $options["pagecount"];//每页显示的条数
        $page = intval($this->_getParam('page'));//默认页码
        $page = empty($page)?'1':$page;
   		$s = $this->_getParam('s'); 
        $ItemCount =  $this->_modelEpg->getCount($s,$contentid);
        $PageCount = ceil($ItemCount/$numPerPage);
        if($page>$PageCount)$page=$PageCount;
        $infos = $this->_modelEpg->getList( $page, $numPerPage, $s,$contentid );
        include $this->_dzTemplate->getFile( 'ajax/epg.manage.ajax.html' );
    }
    /**
    * 添加节目单
    * @return unknown_type
    */
    public function addAction()
    {
    	$contentid = $this->_getParam('contentid'); 
		$t = $this->_getParam('t'); 
    	$request = $this->getRequest();        
        if ($request->isPost())
        {
            $params =$request->getPost();
            $this->_modelEpg->add($params);                
            $this->_redirect("/epg/index/contentid/$contentid/t/$t");
        } 
        include $this->_dzTemplate->getFile( 'epg.add.html' );
    }
    /**
    * 修改节目单
    * @return unknown_type
    */
    public function modifyAction()
    {
    	$contentid = $this->_getParam('contentid'); 
		$t = $this->_getParam('t'); 
    	$epgid = $this->_getParam('epgid');
    	$info = $this->_modelEpg->getInfo($epgid); 
        $request = $this->getRequest();        
        if ($request->isPost())
        {
        	$request =$request->getPost();        	
            if (true)//服务端检查输入参数
            {
                $this->_modelEpg->modify($request,$epgid);
                $this->_redirect("/epg/index/contentid/$contentid/t/$t");
            }
        } 
        include $this->_dzTemplate->getFile( 'epg.edit.html' );
    }
    /**
    * 无刷新修改节目单
    * @return unknown_type
    */    
    public function modifyajaxAction()
    {
        $request = $this->getRequest();
        $params = $request->getPost();
        $arr = split("\|", $params['id']);
        $field = $arr[0];
        $epgid = $arr[1];
        $value = stripslashes($params['value']);
        $this->_modelEpg->modifyAjax($epgid,$field,$value);
        echo $value;
        exit;
    }
    public function modifyajaxreversAction()
    {
    	$field = $this->_getParam('field');
    	$pri = $this->_getParam('pri');
        $this->_modelEpg->modifyAjaxRevers($field,$pri);
        exit;
    }
    /**
    * 删除节目单
    */
    public function deleteAction()
    {
        $epgid = $this->_getParam('epgid');   
        $this->_modelEpg->del($epgid); 
        exit;
    }
    /**
     * 节目单录制
     */
    public function recordAction()
    {
    	$contentid = $this->_getParam('contentid'); 
		$t = $this->_getParam('t'); 
		$t = urlencode($t);
		$title = $this->_getParam('title'); 
    	$epgid = $this->_getParam('epgid');
    	//$info = $this->_modelEpg->getInfo($epgid);//节目信息
    	$categorymodel = new Model_Tables_Category();
    	$cats = $categorymodel->getCategoryNameList(1);
        $request = $this->getRequest();        
        if ($request->isPost())
        {
        	$post =$request->getPost();        	
            if (true)//服务端检查输入参数
            {
                $this->_modelEpg->record($post,$epgid,$this->_userinfo);
                $this->_redirect("/epg/index/contentid/$contentid/t/$t/");
            }
        } 
    	include $this->_dzTemplate->getFile( 'epg.record.html' );
    }
    /**
     * 节目标注
     */
	public function markAction()
    {
    	$uploads = array(
		    	'61999476'=>array('http://111.208.56.34:8001/','/data/'),
		    	'66552247'=>array('http://111.208.56.34:8001/','/data1/'),
		    	'81026058'=>array('http://111.208.56.34:8001/','/data2/'),
		    	'69293957'=>array('http://111.208.56.34:8001/','/data3/'),
		    	'66039375'=>array('http://111.208.56.35:8001/','/data/'),
		    	'73514613'=>array('http://111.208.56.35:8001/','/data1/'),
		    	'64587262'=>array('http://111.208.56.35:8001/','/data2/'),
		    	'28797057'=>array('http://111.208.56.35:8001/','/data3/'));
    	//$edit = $this->_getParam('edit'); 
    	$contentid = $this->_getParam('contentid'); 
		$t = $this->_getParam('t'); 
		$t = urlencode($t);
		$title = $this->_getParam('title'); 
    	$epgid = $this->_getParam('epgid');
    	$info = $this->_modelEpg->getInfo($epgid);//节目信息
    	$markstart = $info['start'];
    	$markend = $info['end'];
    	$categorymodel = new Model_Tables_Category();
    	$cats = $categorymodel->getCategoryNameList(1);
    	$livestreamInfo = $this->_modelContent->getContentStreamInfo('',$contentid);
    	$channelid;
    	$stream=array();
    	foreach($livestreamInfo as $v)
    		{
    			if($v['sourcetype']=='low')
    			{
    				$stream['low']['url']=$v['url'];
    				$stream['low']['uuid']=$v['uuid'];
    			}
    			elseif($v['sourcetype']=='medium')
    			{
    				$stream['medium']['url']=$v['url'];
    				$stream['medium']['uuid']=$v['uuid'];
    				$channelid = $v['channelid'];
    			}
    			elseif($v['sourcetype']=='high')
    			{
    				$stream['high']['url']=$v['url'];
    				$stream['high']['uuid']=$v['uuid'];
    			}
    			else{}
    		}    		
    	$contentInfo = $this->_modelContent->getInfo($contentid);
    	$extendInfo = $this->_modelContent->getExtendInfo($contentInfo["tablename"],$contentid);
    	$uploadInfo = $uploads[$extendInfo["uploader"]]; 
    	$path = $uploadInfo[1];
    	$imgurl = $uploadInfo[0];
        $request = $this->getRequest();        
        if ($request->isPost())
        {
        	$post =$request->getPost();        	
            if (true)//服务端检查输入参数
            {
                $this->_modelEpg->mark($post,$epgid,$this->_userinfo);
                $this->_redirect("/epg/index/contentid/$contentid/t/$t/");
            }
        } 
    	include $this->_dzTemplate->getFile( 'epg.mark.html' );
    }
    /*
     * 根据名称搜索专题，最多50条
     */
    public function getspecialsAction()
    {
    	$name = $this->_getParam('q'); 
    	$rand = time(date());
    	echo $this->_modelEpg->getSpecialsByName($name);exit;
    }
    public function createtvpngAction()
    {
    	$start	= $this->_getParam('start') ;
    	if($start)
    	{
	    	$options = $GLOBALS['application']->getOptions();
	    	$imgsite = $options["imgsite"];//每页显示的条数
	    	$images = $this->_modelEpg->getTvStations($imgsite);
			$rand = time();
	    	$this->implode_image($images,40,25,10);
	    	$title = "台标生成成功！";
		    $info = "<img alt='台标生成成功，时移播放器使用' src='/upload/image/tv.jpg?{$rand}'>";
    	}    	
    	else 
    	{
    		$title = "重新生成台标";
	        $info = "<a href='/epg/createtvpng/start/1'>重新生成台标</a>";
    	}
		header ( 'Content-type:text/html' );
		include $this->_dzTemplate->getFile( 'main.html' );
		//exit;
    }
    /*
     * 合并图形
     */
	function implode_image($images,$imgwidth,$imgheight,$rowcount) {
			header ( 'Content-type:image/jpeg' );
			if (! is_array ( $images ) || count ( $images ) <= 1) {
				return false;
			}
			$max_index = 0;
			$images_info = array ();
			foreach ( $images as $index=>$image ) {
				if($index>$max_index)
					$max_index = $index;
			}
			$max_index +=1;
			$rownum = ceil($max_index/$rowcount);
			$total_height = 0;
			$total_width = 0;
			if($rownum >1){
				$total_width = $rowcount * $imgwidth;
				$total_height = $rownum * $imgheight;
			}
			else
			{
				$total_width = $max_index * $imgwidth;
				$total_height = $imgheight;
			}			
			if (function_exists ( 'imagecreatetruecolor' )) 
			{
				$dst_im = imagecreatetruecolor ( $total_width, $total_height );
			} else
			{
				$dst_im = imagecreate ( $total_width, $total_height );
			}
			foreach ( $images as $index=>$image ) {
				$startX = ($index % $rowcount)*$imgwidth;
				$startY =Floor($index/$rowcount)*$imgheight;
				$src_im = imagecreatefromjpeg ( $image );
				imagecopymerge ( $dst_im, $src_im, $startX, $startY, 0, 0, $imgwidth, $imgheight, 100 );
				imagedestroy ( $src_im );
			}
			imagejpeg ( $dst_im ,'/data/webroot/webcms/public/upload/image/tv.jpg');	
	}
}